<template>
  <!-- 文章详情页 -->
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar title="黑马头条" class="page-nav-bar">
      <van-icon slot="left" name="arrow-left" @click="$router.go(-1)" />
    </van-nav-bar>

    <div class="main-warp">
      <!-- 加载中 -->
      <div v-if="isLoading" class="load-warp">
        <van-loading color="#3296fa" vertical>加载中...</van-loading>
      </div>
      <!-- 加载完成 -->
      <div v-else-if="article.title" class="article-detail">
        <!-- 文章标题 -->
        <h1 class="article-title">{{article.title}}</h1>

        <!-- 用户信息 -->
        <van-cell class="user-info" :border="false" center>
          <van-image class="avatar" round fit="cover" slot="icon" :src="article.aut_photo" />
          <div slot="title" class="user-name">{{article.aut_name}}</div>
          <div slot="label" class="publish-date">{{article.pubdate | dateFormate}}</div>
          <!-- 关注组件 -->
          <!-- <followUser :is_followed="article.is_followed" :user_id="article.aut_id"
            @updateFollow="article.is_followed=$event" /> -->
          <!-- 父组件向子组件传值  且子组件要修改传入的值  可以采取v-model绑定数据
            且v-model解析成 :value="传入数据" @input="修改监听数据方法" -->
          <followUser v-model="article.is_followed" :user_id="article.aut_id" />
        </van-cell>

        <!-- 文本内容 -->
        <div class="article-content markdown-body" v-html="article.content"></div>
        <van-divider>正文介绍</van-divider>
      </div>

      <!-- 加载失败404 -->
      <div v-else-if="errorStatus===404" class="error-warp">
        <van-icon name="failure" />
        <p class="text">该资源已被删除或不存在!</p>
      </div>

      <!-- 其他类型加载失败 例如网络原因或服务端异常 -->
      <div v-else class="error-warp">
        <van-icon name="failure" />
        <p class="text">该资源已被删除或不存在!</p>
        <van-button class="retry-btn">点击重试</van-button>
      </div>

      <!-- 文章评论 -->
      <comment-article :list="commentList" :source="article.art_id" @updaTotal="totalCount=$event"
        @replyShow="replyShow" />
    </div>

    <!-- 底部区域 -->
    <div v-if="article.title" class="article-bottom">
      <van-button type="default" class="comment-btn" size="small" @click="showPop=true" round>写评论</van-button>
      <van-icon color="#777" name="comment-o" :badge="totalCount" />
      <!-- 收藏 -->
      <collect-article v-model="article.is_collected" :articleId="article.art_id" />
      <!-- 点赞 -->
      <like-article v-model="article.attitude" :articleId="article.art_id" @defalutLikeValue="article.attitude=$event"
        @updateLikeValue="article.attitude=$event" />
      <van-icon color="#777" name="share" />
    </div>

    <!-- 评论弹出层 -->
    <van-popup v-model="showPop" position="bottom">
      <pop-field :target="articleId.com_id" :articleId="articleId" @postComment="postComment" />
    </van-popup>

    <!-- 回复弹层 -->
    <van-popup v-model="isReplyShow" position="bottom" :style="{ height: '73%' }" round>
      <comment-reply v-if="isReplyShow" :replyComment="replyComment" @close="isReplyShow=false" />
    </van-popup>
  </div>
</template>

<script>
// 导入接口
import { getArticleList } from '@/api/article.js'
// 传入组件
import followUser from '@/components/follow-user'
import collectArticle from './componets/collect-article.vue'
import LikeArticle from './componets/like-article.vue'
import commentArticle from './componets/comment-article.vue'
// 评论输入组件
import PopField from './componets/pop-field.vue'
// 评论回复
import CommentReply from './componets/comment-reply.vue'
export default {
  name: 'ArticleIndex',
  props: {
    articleId: {
      type: [Number, String, Object],
      require: true
    }
  },
  // provide inject  父级以上祖父级组件向后代传值  层级较深所用
  // provide () {
  //   return {
  //     articleId: this.articleId,
  //     target: this.articleId.com_id
  //   }
  // },
  data () {
    return {
      // 文章详情
      article: {},
      // 加载中
      isLoading: true,
      // 404状态错误
      errorStatus: '',
      totalCount: 0,
      // 弹出层
      showPop: false,
      // 评论列表
      commentList: [],
      // 回复弹层
      isReplyShow: false,
      // 回复评论
      replyComment: {}
    }
  },
  methods: {
    // 回复弹层监听事件
    replyShow (e) {
      console.log(e)
      this.replyComment = e
      this.isReplyShow = true
    },
    // 监听发布评论
    postComment (e) {
      // console.log(e)
      // 添加最新评论
      this.showPop = false
      this.commentList.unshift(e.data.new_obj)
    },
    // 加载文章详情
    async loadArticleList () {
      try {
        // 随机错误
        // thorw Error()
        const { data: res } = await getArticleList(this.articleId)
        this.article = res.data
        console.log(res.data)
      } catch (err) {
        // console.log(err)
        if (err.response?.status === 404) {
          this.errorStatus = 404
        }
        this.$toast('获取失败')
      }
      this.isLoading = false
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: { followUser, collectArticle, LikeArticle, commentArticle, PopField, CommentReply },
  created () {
    // console.log(this.$route)
    this.loadArticleList()
  },
  mounted () { }
}
</script>

<style lang='less' scoped>
.article-container {
  // 导航按钮
  .van-nav-bar {
    position: fixed;
    right: 0;
    left: 0;
    top: 0;
    z-index: 9999;
  }
  /deep/.van-nav-bar .van-icon {
    color: #fff;
  }

  .main-warp {
    position: fixed;
    right: 0;
    left: 0;
    top: 92px;
    bottom: 88px;
    overflow-y: scroll;
    background-color: #fff;
    .article-detail {
      .article-title {
        font-size: 40px;
        padding: 50px 32px;
        margin: 0;
        color: #3a3a3a;
      }

      .user-info {
        padding: 0 32px;
        .avatar {
          width: 70px;
          height: 70px;
          margin-right: 17px;
        }
        .van-cell__label {
          margin-top: 0;
        }
        .user-name {
          font-size: 24px;
          color: #3a3a3a;
        }
        .publish-date {
          font-size: 24px;
          color: #b7b7b7;
        }
        .btn {
          width: 157px;
          height: 58px;
        }
      }

      .article-content {
        padding: 55px 32px;
        /deep/ p {
          // word-wrap: break-word;
          // 两端对齐
          text-align: justify;
        }
      }
    }
  }

  .load-warp {
    padding: 200px 32px;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #fff;
  }

  .error-warp {
    padding: 200px 32px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background-color: #fff;
    .van-icon {
      font-size: 122px;
      color: #b4b4b4;
    }
    .text {
      margin: 30px 0 46px;
      font-size: 30px;
      color: #666;
    }
    .retry-btn {
      width: 280px;
      height: 70px;
      line-height: 70px;
      border: 1px solid #c3c3c3;
      font-size: 30px;
      color: #666;
    }
  }

  .article-bottom {
    position: fixed;
    right: 0;
    left: 0;
    bottom: 0;
    display: flex;
    justify-content: space-around;
    align-items: center;
    box-sizing: border-box;
    height: 88px;
    border-top: solid 1px #d8d8d8;
    background-color: #fff;
    .comment-btn {
      width: 282px;
      height: 46px;
      line-height: 46px;
      font-size: 30px;
      border: solid 2px #ccc;
      color: #a7a7a7;
    }
    .van-icon {
      font-size: 40px;
      .van-info {
        font-size: 16px;
        background-color: #e22829;
      }
    }
  }
}
</style>
